Skip to content

ModuleInfoModel

Base classes

Name Children Inherits
TreeModel
prettyqt.itemmodels.treemodel
TreeModel which is used as a base for most TreeModels implemented.

⋔ Inheritance diagram

graph TD
  3179139764["itemmodels.ModuleInfoModel"]
  2846913429["itemmodels.TreeModel"]
  874904086["core.AbstractItemModel"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2846913429 --> 3179139764
  874904086 --> 2846913429
  3882849576 --> 874904086
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  1898060022 --> 874904086
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: TreeModel

Tree Model to display a module hierarchy (using pkgutil).

Source code in prettyqt/itemmodels/moduleinfomodel.py
class ModuleInfoModel(itemmodels.TreeModel):
    """Tree Model to display a module hierarchy (using pkgutil)."""

    HEADER: ClassVar = ["Name", "Path", "Is Package"]
    SUPPORTS = str | os.PathLike[str] | types.ModuleType | pkgutil.ModuleInfo

    def __init__(self, obj, **kwargs):
        match obj:
            case types.ModuleType():
                # packages have __path__, plain modules only __file__
                paths = getattr(obj, "__path__", None)
                file = paths[0] if paths else obj.__file__
                if file is None:
                    raise TypeError(obj)
                obj = _module_info_for_path(pathlib.Path(file))
            case str() | os.PathLike():
                obj = _module_info_for_path(pathlib.Path(obj))
            case pkgutil.ModuleInfo():
                pass
            case _:
                raise TypeError(obj)
        super().__init__(obj, **kwargs)

    @core.Enum
    class Roles(enum.IntEnum):
        ModuleInfoRole = constants.USER_ROLE + 2

    def columnCount(self, parent=None):
        return len(self.HEADER)

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role, section:
            case constants.HORIZONTAL, constants.DISPLAY_ROLE, _:
                return self.HEADER[section]
        return None

    def data(self, index: core.ModelIndex, role=constants.DISPLAY_ROLE):
        if not index.isValid():
            return None
        info = self.data_by_index(index).obj
        match role, index.column():
            case constants.DISPLAY_ROLE, 0:
                return info.name
            case constants.DISPLAY_ROLE, 1:
                return str(info.module_finder.path)
            case constants.CHECKSTATE_ROLE, 2:
                return self.to_checkstate(info.ispkg)
            case self.Roles.ModuleInfoRole, _:
                return info
        return None

    @classmethod
    def supports(cls, instance) -> bool:
        return isinstance(instance, pkgutil.ModuleInfo | types.ModuleType)

    @staticmethod
    def _get_package_path(info: pkgutil.ModuleInfo) -> str | None:
        if not info.ispkg:
            return None
        if (path := getattr(info.module_finder, "path", None)) is None:
            return None
        return os.path.join(path, info.name)  # noqa: PTH118

    def _fetch_object_children(
        self, item: ModuleInfoModel.TreeItem
    ) -> list[ModuleInfoModel.TreeItem]:
        if (path := self._get_package_path(item.obj)) is None:
            return []
        return [self.TreeItem(obj=i) for i in pkgutil.iter_modules([path])]

    def _has_children(self, item: ModuleInfoModel.TreeItem) -> bool:
        if (path := self._get_package_path(item.obj)) is None:
            return False
        return any(True for _ in pkgutil.iter_modules([path]))

:fa-brands-github: Show source on GitHub